﻿namespace Common
{
    using UnityEngine;
    using HuaweiARUnitySDK;
    using System.Text;

    public class HealthVisualizer : MonoBehaviour
    {
        private ARFace m_face;
        StringBuilder sb = new StringBuilder();
        public void Initialize(ARFace face)
        {
            m_face = face;
            Update();
        }

        public void Update()
        {
            sb.Remove(0, sb.Length);
            sb.Append("No Data");
            if (null == m_face)
            {
                return;
            }
            else if(m_face.GetTrackingState() == ARTrackable.TrackingState.PAUSED)
            {
                //disable face renderer
                return;
            }
            else if (m_face.GetTrackingState() == ARTrackable.TrackingState.STOPPED)
            {
                Destroy(gameObject);
                return;
            }
            //update according to Face parameter
            sb.Remove(0, sb.Length);


            var faceHealthParameters = m_face.GetParameters();

            if (faceHealthParameters == null)
            {
                sb.Append("No health parameters at present!");
            }
            else
            {
                sb.Append("Health parameters:");
                sb.Append("\n");
                foreach (var eachFacePar in faceHealthParameters)
                {
                    sb.Append(eachFacePar.Key);
                    sb.Append(": ");
                    sb.Append(eachFacePar.Value);
                    sb.Append("\n");
                }
            }
        }

        public void OnGUI()
        {
            GUIStyle bb = new GUIStyle();
            bb.normal.background = null;
            bb.normal.textColor = new Color(1, 0, 0);
            bb.fontSize = 24;
            GUI.Label(new Rect(0, 0, 200, 200), sb.ToString(), bb);
        }
    }
}
